//
//  PMViewController.m
//  Anyair
//
//  Created by karlcool on 14-5-14.
//  Copyright (c) 2014年 Karlcool. All rights reserved.
//

#import "PMViewController.h"


@interface PMViewController ()

@end

@implementation PMViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id)initWithType:(int)type {
    
    if ([super init]) {
        dataType = type;
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor lightGrayColor];
    
    UIImage *menuImage = [UIImage imageNamed:@"Menu"];
    UIImage *backImage = [UIImage imageNamed:@"back"];
    
    LRTopBar *topBar;
    
    if (dataType==0) {
        topBar = [[LRTopBar alloc]initWithTitle:@"PM2.5检测" AndLeft:backImage AndRight:menuImage];
    } else if (dataType==1) {
        topBar = [[LRTopBar alloc]initWithTitle:@"缺氧检测" AndLeft:backImage AndRight:menuImage];
    } else if (dataType==2) {
        topBar = [[LRTopBar alloc]initWithTitle:@"甲醛检测" AndLeft:backImage AndRight:menuImage];
    }
    
    
    topBar.delegete = self;
    [self.view addSubview:topBar];
    
    CenterTabBar *tabbar=[[CenterTabBar alloc]initWithFrame:[UIScreen mainScreen].bounds];
    if (!IOS_7) {
        tabbar.frame = CGRectMake(tabbar.frame.origin.x, [UIScreen mainScreen].bounds.size.height-20-tabbar.frame.size.height, tabbar.frame.size.width, tabbar.frame.size.height);
    }
    tabbar.delegate=self;
    [self.view addSubview:tabbar];
    
    childView = [[UIView alloc]initWithFrame:CGRectMake(0, topBar.frame.origin.y+topBar.frame.size.height, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-tabbar.frame.size.height-topBar.frame.size.height-topBar.frame.origin.y)];
    if (!IOS_7) {
        childView.frame = CGRectMake(0, topBar.frame.origin.y+topBar.frame.size.height-20, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-tabbar.frame.size.height-topBar.frame.size.height-topBar.frame.origin.y-20);
    }
    [self.view addSubview:childView];
    
    detection = [[DetectionViewController alloc]initWithType:dataType];
    rim = [[RimViewController alloc]initWithType:dataType];
    history = [[HistoryViewController alloc]initWithType:dataType];
    co2History = [[CO2HistoryViewController alloc]initWithType:dataType];
    [self addChildViewController:detection];
    [self addChildViewController:rim];
    [self addChildViewController:history];
    [self addChildViewController:co2History];
    
    currentViewController=detection;
    [childView addSubview:detection.view];
    viewTag = 1;
    [self.view bringSubviewToFront:tabbar];
    
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - tabbar代理
-(void)FirstBtnClick{
    
    if(viewTag==1)
        return;
    [self transitionFromViewController:currentViewController toViewController:detection duration:0 options:0 animations:^{
    }  completion:^(BOOL finished) {
        currentViewController=detection;
        viewTag=1;
        
    }];
}
-(void)SecondBtnClick{
    if(viewTag==2)
        return;
    
    shareView = [[ShareBoard alloc]initWithFrame:self.view.bounds];
    shareView.delegate = self;
    [self.view addSubview:shareView];
    
}
-(void)ThirdBtnClick{
    if(viewTag==3)
        return;
    [self transitionFromViewController:currentViewController toViewController:rim duration:0 options:0 animations:^{
    }  completion:^(BOOL finished) {
        currentViewController=rim;
        viewTag=3;
        
    }];
}
-(void)FourthBtnClick{
    if(viewTag==4)
        return;
    if (dataType==1) {
        [self transitionFromViewController:currentViewController toViewController:co2History duration:0 options:0 animations:^{
        }  completion:^(BOOL finished) {
            currentViewController=co2History;
            viewTag=4;
            
        }];
    } else {
        [self transitionFromViewController:currentViewController toViewController:history duration:0 options:0 animations:^{
        }  completion:^(BOOL finished) {
            currentViewController=history;
            viewTag=4;
            
        }];
    }
    
}

-(void)CenterBtnClick {
    NSString *userPlace;
    if ([LRUserManager createManager].userAddress.length!=0) {
        userPlace = [NSString stringWithFormat:@"当前位置:%@",[LRUserManager createManager].userAddress];
    } else {
        userPlace = @"当前位置:未知地址";
    }
    
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:userPlace message:@"您只需要输入当前地点(例如:咖啡厅、商场),即可让大家知道此处的环境质量!"delegate:self cancelButtonTitle:@"提交" otherButtonTitles: nil];
    alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
    [alertView show];
    
}

#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex==0) {
        UITextField *tf=[alertView textFieldAtIndex:0];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"updatePlaceDiy" object:tf.text];
    }
}

#pragma mark - 顶栏代理
#pragma mark - 退出数据界面
- (void)clickLeft:(UIButton*)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
    
}
#pragma mark - 展开侧边栏
- (void)clickRight:(UIButton*)sender {
    
    [[SliderBar createSliderBar]showSideBarInViewController:self];
}

#pragma mark - ShareDelegate

- (void)clickShareBtn:(UIButton*)sender {
    
    switch (sender.tag) {
        case 0:

            break;
        case 1:

            break;
        case 2:
            
            break;
        case 3:
            
            break;
        case 4:
            
            break;
        case 5:
            
            break;
        case 6:
            
            break;
        case 7:
            
            break;
        case 8:
            [UIView animateWithDuration:0.3 animations:^{
                shareView.alpha = 0;
            } completion:^(BOOL finished) {
                [shareView removeFromSuperview];
                shareView = nil;
            }];
            break;
    }
}

@end
